package net.uman.web.front;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.uman.entity.Article;
import net.uman.entity.ReviewMessage;
import net.uman.framework.modules.orm.Page;
import net.uman.framework.modules.orm.PropertyFilter;
import net.uman.framework.modules.security.springsecurity.SpringSecurityUtils;
import net.uman.framework.modules.web.struts2.Struts2Utils;
import net.uman.service.ArticleManager;
import net.uman.service.ReviewMessageManager;
import net.uman.web.CrudActionSupport;
@Namespace("/front")
public class FrontNewsAction extends CrudActionSupport<Article> {
	private static final long serialVersionUID = -9028488786516538418L;
	@Autowired
	private ArticleManager articleManager;
	@Autowired
	private ReviewMessageManager reManager;
	
	public Article article;
	
	public String show(){
		try {
			Long articleId = Long.parseLong(Struts2Utils.getParameter("articleId"));
			article = articleManager.get(articleId);
			return "show";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	public void reviewData(){
		try {
			String articleId = Struts2Utils.getParameter("articleId");
			List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
			filters.add(new PropertyFilter("filter_EQL_articleId", articleId));
			Page<ReviewMessage> re_page = new Page<ReviewMessage>(5);
			re_page.setPageNo(pageNum);
			re_page = reManager.findPage(re_page,filters);
			JSONObject jsonObject = new JSONObject();
			JSONArray jsonArray = new JSONArray();
			for(ReviewMessage reMessage : re_page.getResult()){
				JSONObject jObject = new JSONObject();
				jObject.put("", reMessage.getContent());
				jObject.put("", reMessage.getReviewDate());
				jObject.put("", reMessage.getReviewUserName());
				jsonArray.add(jObject);
			}
			jsonObject.put("rows", jsonArray);
			jsonObject.put("total", re_page.getTotalCount());
			Struts2Utils.renderJson(jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void review(){
		try {
			user = SpringSecurityUtils.getCurrentUser();
			if(user!=null){
				String msgString = Struts2Utils.getParameter("reviewMsg");
				Long articleId = Long.parseLong(Struts2Utils.getParameter("articleId"));
				if(!StringUtils.isEmpty(msgString)&&articleId!=null){
					ReviewMessage reviewMessage = new ReviewMessage();
					reviewMessage.setArticleId(articleId);
					reviewMessage.setContent(msgString);
					reviewMessage.setReviewDate(new Date(System.currentTimeMillis()));
					reviewMessage.setReviewUserId(user.getId());
					reviewMessage.setReviewUserName(user.getName());
					reManager.save(reviewMessage);
					Struts2Utils.renderText("success");
				}else{
					Struts2Utils.renderText("系统正忙，请稍候再试！");
				}
			}else{
				Struts2Utils.renderText("nouser");
			}
		} catch (Exception e) {
			e.printStackTrace();
			Struts2Utils.renderText("系统正忙，请稍候再试！");
		}
	}
	
	public Article getModel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		
	}

}
